package net.acoder.util;

public class StringUtil {
	/**
	 * Compare <code>str1</code> to <code>str2</code>. The result is
	 * <code>true</code> if the arguments are null or the arguments has the same
	 * sequence.
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean equal(String str1, String str2) {
		if (str1 == null) {
			return str2 == null;
		}
		return str1.equals(str2);
	}

	/**
	 * for example, if src = "java hello world", return "world hello java";
	 * 
	 * @param src
	 * @param splitChar
	 * @return
	 */
	public static String reverseWord(String src, char splitChar) {
		if (src == null || src.length() == 0) {
			return src;
		}
		char[] cs = src.toCharArray();
		String temp = "";
		StringBuffer buf = new StringBuffer();
		for (int l = cs.length - 1; l >= 0; l--) {
			char c = cs[l];
			if (c != splitChar) {
				temp = c + temp;
			} else {
				buf.append(temp).append(c);
				temp = "";
			}
		}
		return buf.toString();
	}

	public static String center(String str, int size) {
		return center(str, size, ' ');
	}

	public static String center(String str, int size, char padChar) {
		if (str == null) {
			return null;
		}
		if (str.length() >= size) {
			return str;
		}
		int pre = (size - str.length()) / 2;
		int sub = size - str.length() - pre;
		StringBuffer buf = new StringBuffer(size);
		for (int i = 0; i < pre; i++) {
			buf.append(padChar);
		}
		buf.append(str);
		for (int i = 0; i < sub; i++) {
			buf.append(padChar);
		}
		return buf.toString();
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static String capitalize(String str) {
		if (isEmpty(str)) {
			return str;
		}
		char[] cs = str.toCharArray();
		cs[0] = Character.toUpperCase(cs[0]);
		return new String(cs);
	}

	public static String uncapitalize(String str) {
		if (isEmpty(str)) {
			return str;
		}
		char[] cs = str.toCharArray();
		cs[0] = Character.toLowerCase(cs[0]);
		return new String(cs);
	}

	public static String chop(String str) {
		if (isEmpty(str)) {
			return str;
		}
		return str.substring(0, str.length() - 1);
	}

	public static String repeat(String str, int repeat) {
		if (isEmpty(str) || repeat <= 1) {
			return str;
		}
		StringBuffer buf = new StringBuffer();
		while (repeat-- > 0) {
			buf.append(str);
		}
		return buf.toString();
	}

	public static String rightPad(String str, int size) {
		if (str == null || size <= str.length()) {
			return str;
		}
		int diff = size - str.length();
		StringBuffer buf = new StringBuffer(size);
		buf.append(str);
		for (int i = 0; i < diff; i++) {
			buf.append(' ');
		}
		return buf.toString();
	}

	public static String leftPad(String str, int size) {
		if (str == null || size <= str.length()) {
			return str;
		}
		int diff = size - str.length();
		StringBuffer buf = new StringBuffer(size);
		for (int i = 0; i < diff; i++) {
			buf.append(' ');
		}
		buf.append(str);
		return buf.toString();
	}

	public static int length(String str) {
		return isEmpty(str) ? 0 : str.length();
	}

	public static String swapCase(String str) {
		if (isEmpty(str)) {
			return str;
		}
		char[] cs = str.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			if (Character.isLowerCase(cs[i])) {
				cs[i] = Character.toUpperCase(cs[i]);
			} else if (Character.isUpperCase(cs[i])) {
				cs[i] = Character.toLowerCase(cs[i]);
			}
		}
		return new String(cs);
	}

	public static String reverse(String str) {
		if (isEmpty(str)) {
			return str;
		}
		char[] cs = str.toCharArray();
		for (int i = 0; i < cs.length / 2; i++) {
			char temp = cs[i];
			cs[i] = cs[cs.length - 1 - i];
			cs[cs.length - 1 - i] = temp;
		}
		return new String(cs);
	}

	public static void main(String[] args) {
		String src = "a aaa aaaa    b";
		System.out.println(reverseWord(src, ' '));
	}
}
